//
//  CQVBToolBar.m
//  CQPictureBrowser
//
//  Created by 陈长青 on 2017/3/30.
//  Copyright © 2017年 chenchangqing198@126.com. All rights reserved.
//

#import "CQVBToolBar.h"
#import "CQVideoCell.h"

@interface CQVBToolBar ()

@property (weak, nonatomic) IBOutlet UIButton *togglePlaybackButton;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *scrubberSlider;

// 显示文字

@property (weak, nonatomic) UILabel *textLabel;

// 播放器

@property (nonatomic, weak) CQVideoPlayer *videoPlayer;

@end

@implementation CQVBToolBar
@synthesize scrubbing=_scrubbing;
@synthesize cell=_cell;

#pragma mark - 生命周期

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.scrubberSlider addTarget:self action:@selector(scrubbingDidStart) forControlEvents:UIControlEventTouchDown];
    [self.scrubberSlider addTarget:self action:@selector(scrubing) forControlEvents:UIControlEventValueChanged];
    [self.scrubberSlider addTarget:self action:@selector(scrubbingDidEnd) forControlEvents:UIControlEventTouchUpInside];
    [self.scrubberSlider addTarget:self action:@selector(scrubbingDidEnd) forControlEvents:UIControlEventTouchUpOutside];
    [self.scrubberSlider addTarget:self action:@selector(scrubbingDidEnd) forControlEvents:UIControlEventTouchCancel];
}

- (CQVideoPlayer *)videoPlayer {
    
    if ([_cell isKindOfClass:[CQVideoCell class]]) {
        
        CQVideoCell *videoCell = (CQVideoCell *)_cell;
        
        return videoCell.videoPlayer;
    }
    return nil;
}

#pragma mark - 协议实现

//更新播放进度

-(void)setCurrentTime:(NSTimeInterval)time duration:(NSTimeInterval)duration {
    
    // 错误处理
    time = isnan(time) ? 0.0 : time;
    duration = isnan(duration) ? 0.0 :duration;
    
    NSInteger currentSeconds = ceilf(time);
    double remainingTime = duration - time;
    self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",[self formatSeconds:currentSeconds],[self formatSeconds:remainingTime]];
    self.scrubberSlider.minimumValue = 0.0f;
    self.scrubberSlider.maximumValue = duration;
    self.scrubberSlider.value = time;
}

//处理播放完成

-(void)playbackComplete {
    
    self.scrubberSlider.value = 0.0f;
    self.togglePlaybackButton.selected = NO;
}

//处理播放暂停

-(void)setToPlaying:(BOOL) isPlaying {
    
    self.togglePlaybackButton.selected = isPlaying;
}

#pragma mark - Actions

- (void)scrubbingDidStart {
    
    self.scrubbing = YES;
//    [self.viewBrowser resetTimer];
    [self.videoPlayer scrubbingDidStart];
}

- (void)scrubing {
    
    NSTimeInterval currentTime = self.scrubberSlider.value;
    NSTimeInterval duration = self.scrubberSlider.maximumValue;
    [self setCurrentTime:currentTime duration:duration];
    if (self.videoPlayer.player) {
        
        [self.videoPlayer scrubbedToTime:currentTime];
    } else {
        
        _scrubberSlider.value = 0;
    }
}

- (void)scrubbingDidEnd {
    
    self.scrubbing = NO;
//    [self.viewBrowser resetTimer];
    [self.videoPlayer scrubbingDidEnd];
}

- (IBAction)togglePlayback:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.videoPlayer) {
        if (sender.selected) {
            [self.videoPlayer play];
        } else {
            [self.videoPlayer pause];
        }
    }
}

#pragma mark - 私有方法

- (NSString *)formatSeconds:(NSInteger)value {
    NSInteger seconds = value % 60;
    NSInteger minutes = value / 60;
    return [NSString stringWithFormat:@"%02ld:%02ld", (long) minutes, (long) seconds];
}


@end
